<?php
namespace app\admin\controller;
use app\admin\model\Goods as GoodsModel;
use app\admin\model\Classify as ClassifyModel;

//商品管理类
class Goods extends Base
{
    /**
     * 获取商品列表
     */
    public function lst()
    {
        $report       = input('report');
        $re_status    = input('re_status');
        $goods_status = input('goods_status');

        $where  = array();
        if (!empty($report)) {
            if ($report == 1) {
                $where['gd_report_num']  = ['=',0];
            } elseif($report == 2) {
                $where['gd_report_num']  = ['>',0];
            }

        }

        if (!empty($re_status)) {
            $where['gd_re_status']  = ['=',$re_status];
        }

        if (!empty($goods_status)) {
            $where['gd_goods_status']  = ['=',$goods_status];
        }

        $list = GoodsModel::where($where)->field('gd_del_aid,gd_del_note,gd_del_time',true)->order('gd_create_time DESC')->paginate(10);

        $data = displayJson($list);

        $arr  = json_decode($data,true);

        $newArr = $arr['data'];

        $userIds    = array(); //用户id数组
        $catArr     = array(); //分类数组
        foreach ($newArr as $key => $val) {
            $userIds[]                  = $val['gd_uid'];
            $catArr[$val['gd_id']]      = $val['gd_classify'];
        }

        $user_list   = $this->getUserEmailByIds($userIds);
        $cat_list    = $this->getClassifByGdIds($catArr);

        foreach ($newArr as $key => &$val) {
            $newArr[$key]['us_status']   = $user_list[$val['gd_uid']]['us_status'];
            $newArr[$key]['gd_uid']      = $user_list[$val['gd_uid']]['us_email'];
            $newArr[$key]['gd_classify'] = $cat_list[$val['gd_id']];

        }

        $arr['data'] = $newArr;

        $info['code'] = '200';
        $info['data'] = $arr;

        echo displayJson($info);die;
    }

    /**
     * 获取商品详情
     */
    public function getGoodsDesc()
    {
        $gid = input('gid');

        $getRow = db('goods',[],false)->where('gd_id',$gid)->find();

        if ($getRow['gd_del_aid']) {
            $getAdmin = db('admin',[],false)->where('ad_id',$getRow['gd_del_aid'])->find();

            $getRow['gd_del_aid'] = $getAdmin['ad_account'];
        }

        $userInfo = $this->getUserEmail($getRow['gd_uid']);
        $uid       = $getRow['gd_uid'];

        $getRow['gd_uid']      = $userInfo[$uid]['us_email'];
        $getRow['us_status']   = $userInfo[$uid]['us_status'];
        $getRow['gd_classify'] = $this->getClassifyById($getRow['gd_classify']);


        if ($getRow) {
            $info['code'] = '200';
            $info['data'] = $getRow;
            echo displayJson($info);die;
        } else {
            $info['code']    = '400';
            $info['message'] = '获取失败';
            echo displayJson($info);die;
        }
    }

    /**
     * 删除商品
     */
    public function delGoods()
    {
        $gid  = input('gid'); //商品id
        $aid  = input('aid'); //管理员id
        $note = htmlspecialchars(input('note')); //删除原因

        if (empty($gid) || empty($aid)) {
            $info['code']    = '401';
            $info['message'] = '重要参数缺失，系统自动退出';
            echo displayJson($info);die;
        }

        if (empty($note)) {
            $info['code']    = '400';
            $info['message'] = '删除原因必须填写';
            echo displayJson($info);die;
        }

        $getGoods = db('goods',[],false)->where('gd_id',$gid)->find();

        if ($getGoods['gd_status'] == 2) {
            $info['code']    = '400';
            $info['message'] = '该商品已被移除,请刷新页面查看';
            echo displayJson($info);die;
        }

        $set['gd_status']    = 2;
        $set['gd_del_aid']   = $aid;
        $set['gd_del_note']  = $note;
        $set['gd_del_time']  = time();

        $res = db('goods',[],false)->where('gd_id',$gid)->update($set);

        if ($res) {
            //发送邮件告知用户
            $getUser = db('user',[],false)->where('us_id',$getGoods['gd_uid'])->find();

            $send_data = array(
                'to_email'   =>$getUser['us_email'],
                'content'    =>'【淘二货】您发布的商品标题：<span style="font-size: 20px;font-weight: bold;"> '.$getGoods['gd_title'].' </span>，已被管理员删除。<br/>删除原因： '.$note.'<br/>如有疑问，请联系管理员QQ：1298025852'
            );

            $send      = sendEmail($send_data);

            if (!$send) {
                $info['code']    = '400';
                $info['message'] = '删除成功,邮件发送失败';
                echo displayJson($info);die;
            } else {
                //用户发布商品数减一
                db('user',[],false)->where('us_id',$getUser['us_id'])->setDec('us_goods_num');

                //分类下的商品数量减一
                db('classify',[],false)->where('cf_id',$getGoods['gd_classify'])->setDec('cf_goods_num');

                $info['code']    = '200';
                $info['message'] = '删除成功';
                echo displayJson($info);die;
            }
        } else {
            $info['code']    = '400';
            $info['message'] = '删除失败';
            echo displayJson($info);die;
        }
    }

    
    /**
     * 还原商品
     */
    public function reductionGoods()
    {
        $gid = input('gid');

        if (empty($gid)) {
            $info['code']    = '401';
            $info['message'] = '重要参数缺失，系统自动退出';
            echo displayJson($info);die;
        }

        $goods  = db('goods',[],false)->where('gd_id',$gid)->find();

        if ($goods['gd_status'] == '1') {
            $info['code']    = '400';
            $info['message'] = '该商品状态已正常，请刷新后查看';
            echo displayJson($info);die;
        }

        $set['gd_status']    = 1;
        $set['gd_del_aid']   = NULL;
        $set['gd_del_note']  = NULL;
        $set['gd_del_time']  = NULL;

        $res = db('goods',[],false)->where('gd_id',$gid)->update($set);

        if ($res) {
            //向用户发送邮件
            $user   = db('user',[],false)->where('us_id',$goods['gd_uid'])->value('us_email');

            $send_data = array(
                'to_email'   =>$user,
                'content'    =>'【淘二货】您发布的二货：<span style="font-size: 20px;font-weight: bold;"> '.$goods['gd_title'].' </span>，状态现已恢复正常。<br/>如有疑问，请联系管理员QQ：1298025852'
            );

            $send      = sendEmail($send_data);

            if (!$send) {
                $info['code']    = '400';
                $info['message'] = '商品状态更新成功,邮件发送失败';
                echo displayJson($info);die;
            } else {
                //用户发布商品数加一
                db('user',[],false)->where('us_id',$goods['gd_uid'])->setInc('us_goods_num');

                $info['code']    = '200';
                $info['message'] = '商品状态更新成功';
                echo displayJson($info);die;
            }
        } else {
            $info['code']    = '400';
            $info['message'] = '商品状态更新失败';
            echo displayJson($info);die;
        }
    }

    /**
     * 商品分类列表
     */
    public function goodsClassifyLst()
    {
        $list = ClassifyModel::order('cf_id ASC')->paginate(10);

        $info['code'] = '200';
        $info['data'] = $list;

        echo displayJson($info);die;
    }

    /**
     * 增加分类
     */
    public function addClassify()
    {
        $data = input('post.');

        if (empty($data['cf_name']) || empty($data['cf_status'])) {
            $info['code']    = '400';
            $info['message'] = '请完善信息';
            echo displayJson($info);die;
        }

        $res = db('classify',[],false)->insert($data);

        if ($res) {
            $info['code']    = '200';
            $info['message'] = '添加成功';
            echo displayJson($info);die;
        } else {
            $info['code']    = '400';
            $info['message'] = '添加失败';
            echo displayJson($info);die;
        }
    }

    /**
     * 编辑分类
     */
    public function editClassify()
    {
        $data = input('post.');

        if (empty($data['cf_id'])) {
            $info['code']    = '401';
            $info['message'] = '重要参数丢失,系统自动退出';
            echo displayJson($info);die;
        }

        if (empty($data['cf_name'])) {
            $info['code']    = '400';
            $info['message'] = '请完善信息';
            echo displayJson($info);die;
        }

        $res = db('classify',[],false)->where('cf_id',$data['cf_id'])->update($data);

        if ($res >= 0) {
            $info['code']    = '200';
            $info['message'] = '编辑成功';
            echo displayJson($info);die;
        } else {
            $info['code']    = '400';
            $info['message'] = '编辑失败';
            echo displayJson($info);die;
        }
    }

    /**
     * 更新分类状态
     */
    public function changeStatus()
    {
        $data = input('post.');

        if (empty($data['cf_id'])) {
            $info['code']    = '401';
            $info['message'] = '重要参数丢失,系统自动退出';
            echo displayJson($info);die;
        }

        if (empty($data['cf_status'])) {
            $info['code']    = '400';
            $info['message'] = '状态不能为空';
            echo displayJson($info);die;
        }

        $res = db('classify',[],false)->where('cf_id',$data['cf_id'])->update($data);

        if ($res) {
            $info['code']    = '200';
            $info['message'] = '更新成功';
            echo displayJson($info);die;
        } else {
            $info['code']    = '400';
            $info['message'] = '更新失败';
            echo displayJson($info);die;
        }
    }

}
